/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.bill.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BaseProvincesMapper;
import com.nmf.dao.gen.BizProjectBillMapper;
import com.nmf.dao.gen.bean.BaseProvinces;
import com.nmf.dao.gen.bean.BizProjectBill;
import com.nmf.dao.gen.bean.BizProjectBillExample;
import com.nmf.site.biz.bill.enums.BillStatusEnums;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;
import com.nmf.site.modules.sys.entity.User;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 工程单据Service
 * @author 北冥
 * @version 2019-01-25
 */
@Service
@Transactional(readOnly = true)
public class BizProjectBillService{

	@Autowired
	BizProjectBillMapper bizProjectBillMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizProjectBill get(String no,String opt) {
		BizProjectBill bizProjectBill= bizProjectBillMapper.selectByPrimaryKey(no);
		
		if(StringUtils.isNotBlank(bizProjectBill.getExplainContent())) {
			String content = StringEscapeUtils.unescapeHtml4(bizProjectBill.getExplainContent());
			bizProjectBill.setExplainContent(content);
		}
		
		return bizProjectBill;
	}
	
	public List<BizProjectBill> findList(BizProjectBill bizProjectBill) {
		BizProjectBillExample bizProjectBillExample = new BizProjectBillExample();
		BizProjectBillExample.Criteria criteria = bizProjectBillExample.createCriteria();
		
		return bizProjectBillMapper.selectByExample(bizProjectBillExample);
	}
	
	public Page<BizProjectBill> findPage(Page<BizProjectBill> page, BizProjectBill bizProjectBill) {
		BizProjectBillExample bizProjectBillExample = new BizProjectBillExample();
		BizProjectBillExample.Criteria criteria = bizProjectBillExample.createCriteria();
		
		if(bizProjectBill.getBillStatus()!=null) {
			criteria.andBillStatusEqualTo(bizProjectBill.getBillStatus());
		}
		bizProjectBillExample.setPage(page);
		
		List<BizProjectBill> dlist = bizProjectBillMapper.selectByExample(bizProjectBillExample);
		page.setList(dlist);
		page.setCount(bizProjectBillMapper.countByExample(bizProjectBillExample));
		
		return page;
	}
	
	public Page<BizProjectBill> findYttPage(Page<BizProjectBill> page, BizProjectBill bizProjectBill) {
		User user = UserUtils.getUser();
		BizProjectBillExample bizProjectBillExample = new BizProjectBillExample();
		BizProjectBillExample.Criteria criteria = bizProjectBillExample.createCriteria();
		
		criteria.andSubmitUserIdEqualTo(user.getId());
		
		bizProjectBillExample.setPage(page);
		
		List<BizProjectBill> dlist = bizProjectBillMapper.selectByExample(bizProjectBillExample);
		page.setList(dlist);
		page.setCount(bizProjectBillMapper.countByExample(bizProjectBillExample));
		
		return page;
	}
	
	
	
	@Transactional(readOnly = false)
	public void save(BizProjectBill bizProjectBill,User user) {
		setInfo(bizProjectBill);
		if(StringUtils.isNotEmpty(bizProjectBill.getId())&&get(bizProjectBill.getId(),null)!=null) {
			bizProjectBillMapper.updateByPrimaryKeySelective(bizProjectBill);
		}else {
			Date date = new Date();
			bizProjectBill.setCreateDate(date);
			bizProjectBill.setId(idGenerater.genClsNo("bizProjectBill"));
			bizProjectBill.setUpdateDate(date);
			bizProjectBill.setBillStatus(BillStatusEnums.INIT.value);
			bizProjectBill.setBillStatusName(BillStatusEnums.INIT.name);
			bizProjectBill.setSubmitUserId(user.getId());
			bizProjectBill.setSubmitUserName(user.getName());
			bizProjectBillMapper.insertSelective(bizProjectBill);
		}
	}
	@Autowired
	BaseProvincesMapper baseProvincesMapper;
	public void setInfo(BizProjectBill bizProjectBill) {
		if(StringUtils.isNotBlank(bizProjectBill.getProvinceId())) {
			BaseProvinces baseProvinces = baseProvincesMapper.selectByPrimaryKey(bizProjectBill.getProvinceId());
			if(baseProvinces!=null) {
				bizProjectBill.setProvinceName(baseProvinces.getProvinceName());
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizProjectBill bizProjectBill) {
		bizProjectBillMapper.deleteByPrimaryKey(bizProjectBill.getId());
	}
	
	
	@Transactional(readOnly = false)
	public BizProjectBill audit(BizProjectBill bizProjectBill,User user,BillStatusEnums billStatusEnums) {
		bizProjectBill.setBillStatus(billStatusEnums.value);
		bizProjectBill.setBillStatusName(billStatusEnums.name);
		
		if(BillStatusEnums.toCountryAudit.equals(billStatusEnums)) {
			bizProjectBill.setProvinceAuditUid(user.getId());
			bizProjectBill.setProvinceAuditName(user.getName());
		}else if(BillStatusEnums.auditSuccess.equals(billStatusEnums)) {
			bizProjectBill.setCountryAuditUid(user.getId());
			bizProjectBill.setCountryAuditName(user.getName());
		}
		bizProjectBillMapper.updateByPrimaryKeySelective(bizProjectBill);
		return bizProjectBillMapper.selectByPrimaryKey(bizProjectBill.getId());
	}
	
}